home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 December / DPPCPRO1205.ISO / Essentials / Programming / Basic4GL / Setup Basic4GL v2.3.1.exe / $INSTDIR / Programs / Starfield6.gb < prev    next >
Encoding:
Text File  |  2005-07-29  |  2.8 KB  |  80 lines

  1. ' Star field demo 6
  2. '
  3. ' Spinning effect, using OpenGL transformations.
  4.  
  5. const maxStars = 500
  6.  
  7. dim stars#(maxStars)(2), starCols(maxStars)(2)
  8. dim i, texture, angle#
  9.  
  10. ' Populate star field
  11. for i = 1 to maxStars
  12.     stars#(i) = vec3 (rnd () % 301 - 150, rnd () % 301 - 150, -i)
  13.     
  14.     ' Choose star colour.
  15.     starCols(i)(0) = rnd () % 256
  16.     starCols(i)(1) = rnd () % 256
  17.     starCols(i)(2) = rnd () % 256
  18. next
  19.  
  20. ' Setup OpenGL fog.
  21. glEnable (GL_FOG)
  22. glFogi (GL_FOG_MODE, GL_LINEAR)             ' Objects fade out linearly
  23. glFogf (GL_FOG_END, maxStars)               ' Objects past this distance are totally faded
  24. glFogf (GL_FOG_START, 0)                    ' Objects before this distance are totally un-faded
  25. glFogfv (GL_FOG_COLOR, vec3 (0, 0, 0))      ' Fog colour = black
  26.  
  27. ' Load star texture.
  28. texture = LoadTexture ("data\star.bmp")
  29.  
  30. ' Enable texture mapping
  31. glEnable (GL_TEXTURE_2D)
  32.  
  33. ' Main loop
  34. while true    
  35.     glClear (GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)    
  36.     glBindTexture (GL_TEXTURE_2D, texture)
  37.     
  38.     ' Spin the view by rotating ALL stars by angle#.
  39.     ' To do this we set up a rotation TRANSFORMATION.
  40.     ' Once the transformation is setup, OpenGL will automatically apply it to anything
  41.     ' we draw from that point onwards. 
  42.     angle# = angle# + .1
  43.     glLoadIdentity ()                   ' Clear out transformations
  44.         ' We have to clear out any existing transformations first, Otherwise OpenGL will COMBINE
  45.         ' the new transformation with any old one(s).
  46.     glRotatef (angle#, 0, 0, 1)         ' Add a rotation transformation, by angle# about the Z axis
  47.         ' The first parameter is the angle (angle#). The last 3 make up the vector to rotate around.
  48.         ' In this case (0, 0, 1), which points along the Z axis (out of the screen).
  49.  
  50.     for i = 1 to maxStars
  51.     
  52.         ' Move the star forward, by adding 1 to Z
  53.         stars#(i) = stars#(i) + vec3 (0, 0, 1)
  54.  
  55.         ' If the Z goes positive (behind the screen), move it to the back again.
  56.         if stars#(i)(2) >= 0 then stars#(i)(2) = -maxStars endif
  57.         
  58.         ' Draw star
  59.         glBegin (GL_QUADS)
  60.         
  61.             ' Set the star's colour.
  62.             glColor3bv (starCols (i))
  63.             
  64.             glTexCoord2f (0, 1)                         ' Top left
  65.             glVertex3fv (stars#(i) + vec3(-1, 1, 0))
  66.  
  67.             glTexCoord2f (0, 0)                         ' Bottom left
  68.             glVertex3fv (stars#(i) + vec3(-1,-1, 0))
  69.             
  70.             glTexCoord2f (1, 0)                         ' Bottom right
  71.             glVertex3fv (stars#(i) + vec3( 1,-1, 0))
  72.  
  73.             glTexCoord2f (1, 1)                         ' Top right
  74.             glVertex3fv (stars#(i) + vec3( 1, 1, 0))
  75.         glEnd ()
  76.     next
  77.     SwapBuffers ()
  78.     WaitTimer (20)
  79. wend
  80.